package com.vito.framework.system.org.service.impl;

import com.vito.framework.base.exception.CommonException;
import com.vito.framework.base.service.impl.BaseServiceImpl;
import com.vito.framework.system.org.mapper.OrgManageMapper;
import com.vito.framework.system.org.model.OrgBean;
import com.vito.framework.system.org.service.OrgManageService;
import com.vito.framework.system.role.model.RoleBean;
import com.vito.framework.system.user.model.UserBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrgManageServiceImpl extends BaseServiceImpl<OrgManageMapper, OrgBean> implements OrgManageService {

	@Resource
	OrgManageMapper orgManageMapper;
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void deleteOrgManage(Map<String, Object> paramMap)
			throws CommonException {
	    List<OrgBean> deleteList = orgManageMapper.getDeleteingOrgManager(paramMap);

		orgManageMapper.deleteOrgManage(paramMap);
		String[] ids = new String[deleteList.size()];
		for (int i = 0; i < deleteList.size(); i++) {
		    ids[i] = deleteList.get(i).getOrgId();
		}
		paramMap.put("ids", ids);
		/*delete user role*/
		orgManageMapper.deleteUserRole(paramMap);
		/*delete user*/
		orgManageMapper.deleteOrgUser(paramMap);
		/*delete role menu*/
		orgManageMapper.deleteRoleMenu(paramMap);
		/*delete role*/
		orgManageMapper.deleteSysRole(paramMap);
		/*delete org menu*/
		orgManageMapper.deleteSysOrgMenu(paramMap);
	}
	
	@Override
	public OrgBean getOrgManageById(Map<String, Object> paramMap) throws CommonException {
		return orgManageMapper.getOrgManageById(paramMap);
	}
	
	@Override
	public List<OrgBean> getOrgManageList(Map<String, Object> paramMap)
			throws CommonException {
		return orgManageMapper.getOrgManageList(paramMap);
	}
	
	@Override
	public List<OrgBean> getOrgManageListById(Map<String, Object> paramMap) throws CommonException {
		return orgManageMapper.getOrgManageListById(paramMap);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void insertOrgManage(OrgBean si) throws CommonException {
		//添加机构
	    orgManageMapper.insertOrgManage(si);
	}
	
	@Override
	public void insertOrgManageList(List<OrgBean> list) throws CommonException {
		orgManageMapper.insertOrgList(list);
	}
	
	@Override
	public String queryTemplateList(Map<String, Object> map)throws CommonException {
		return orgManageMapper.getOrgEditCount(map);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void updateOrgManage(OrgBean si) throws CommonException {
		String oldSiId =  si.getOldOrgId();
		String siId = si.getOrgId();
		//判断原si编号和修改后的是否一致，若一致则直接更新
		if (oldSiId.equals(siId)) {
			orgManageMapper.updateOrgManage(si);
		}else {
			Map<String, Object> map = new HashMap<>();
			map.put("param_orgId", si);
			//查询修改后的si编号是否有重复，重复则提示resultMessage，没有重复则更新si信息
			String count = orgManageMapper.getOrgEditCount(map);
			if(("0").equals(count)) {
				orgManageMapper.updateOrgManage(si);
			} else {
				throw new CommonException("机构编号重复，请重新修改");
			}
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void distributeMenus(Map<String, Object> map) throws CommonException {
		String[] orgIds = (String[]) map.get("ids");
		String menuIdStr = (String) map.get("param_menuIds");
		String param_orgIds = "";
		for(int z = 0; z<orgIds.length; z++){
			if(z == orgIds.length - 1){
				param_orgIds = param_orgIds + orgIds[z];
			}else{
				param_orgIds = param_orgIds + orgIds[z] + ",";
			}
		}
		map.put("param_orgIds", param_orgIds);
		if(String.valueOf(menuIdStr.charAt(menuIdStr.length() -1)).equalsIgnoreCase(",")){
			menuIdStr = menuIdStr.substring(0,menuIdStr.length()-1);
		}
		map.put("param_menuIds", menuIdStr);
		//删除角色下的菜单
		orgManageMapper.deleteRoleMenu(map);
		
		String[] menuIds = menuIdStr.split(",");
		for(int i = 0; i<orgIds.length; i++){
			map.put("orgId", orgIds[i]);
			//删除组织下的所有关联菜单
			orgManageMapper.deleteOrgMenu(map);
			for(int j = 0; j<menuIds.length; j++){
				map.put("menuId", menuIds[j]);
				//根据所选菜单，重新添加组织与菜单对应信息
				orgManageMapper.insertOrgMenu(map);
			}
		}
	}

	@Override
	public List<String> getMenus(Map<String, Object> paramMap) throws CommonException {
		return orgManageMapper.getMenus(paramMap);
	}

    @Override
    public void adminAdd(OrgBean si) throws CommonException {
        String roleId = orgManageMapper.getRoleId();
        RoleBean roleBean = new RoleBean();
        roleBean.setRoleId(roleId);
        roleBean.setRoleName("机构管理员角色");
        roleBean.setOrgId(si.getOrgId());
        roleBean.setRoleType("1");
        //添加权限
        orgManageMapper.insertOrgRole(roleBean);
      //添加角色和菜单
        orgManageMapper.insertRoleMenu(roleBean);
        
        UserBean userBean = new UserBean();
        userBean.setUserName("机构管理员");
        userBean.setAccount(si.getAdminAccount());
        userBean.setPassword(si.getAdminPassword());
        userBean.setOrgId(si.getOrgId());
        userBean.setCityCode(si.getCityCode());
        userBean.setUserType("0");
        userBean.setStatus("1");
        //添加用户
        orgManageMapper.insertOrgUser(userBean);
        roleBean.setUserId(userBean.getUserId());
        //添加用户权限
        orgManageMapper.insertUserRole(roleBean);
    }

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void distributeOrgMenu(String orgId,String[] ids) throws CommonException {
		Map<String,Object> paramMap = new HashMap<String,Object>();
		if(null != ids){
			String[] menuIds = ids;
			for(int i=0;i<menuIds.length;i++){
				paramMap.put("orgId", orgId);
				paramMap.put("menuId", menuIds[i]);
				//baseDao.delete("system.orgManage.deleteOrgMenu", paramMap);//插入之前先从该表中删除已经存在的数据
				orgManageMapper.deleteOrgMenu(paramMap);
			}
			for(int i=0;i<menuIds.length;i++){
				paramMap.put("orgId", orgId);
				paramMap.put("menuId", menuIds[i]);
				//baseDao.insert("system.orgManage.insertOrgMenu", paramMap);//插入之前先从该表中删除已经存在的数据
				orgManageMapper.insertOrgMenu(paramMap);
			}
		}
	}

	@Override
	public String getOrgLevel(String orgId) {
		return orgManageMapper.getOrgLevel(orgId);
	}

	@Override
	public List<OrgBean> getOrgManageByMenuId(String menuId) throws CommonException {
		return orgManageMapper.getOrgManageByMenuId(menuId);
	}

}
